/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.scatterplot;

import java.util.Collection;
import java.util.Vector;

import controlP5.ControlP5;
import controlP5.ControllerGroup;

import de.fuberlin.processing.who.cancer.data.structs.Struct_14_LifeExpectancyToUvRadiation;
import de.fuberlin.processing.who.cancer.data.structs.Struct_15_CancerMortalityToAge;
import de.fuberlin.processing.who.cancer.interaction.LocationBroadcaster;
import de.fuberlin.processing.who.cancer.interaction.LocationModifier;
import processing.core.PApplet;


public class Cell_15_CancerMortalityToAge extends CustomCell {

	public Cell_15_CancerMortalityToAge( ControlP5 theControlP5,
			ControllerGroup theParent, String theName,
			CellConstructionAndProcessingParcel cellConstructionAndProcessingParcel,
			Collection coll, float theX, float theY, int theWidth, int theHeight) {
		super( theControlP5, theParent, theName, cellConstructionAndProcessingParcel,
				coll, theX, theY, theWidth, theHeight);
		this.locationModifier = LocationBroadcaster.get();
	}

	private LocationModifier locationModifier;

	@Override
	public void drawCell(PApplet p) {
		

		Vector<Struct_15_CancerMortalityToAge> data = (Vector<Struct_15_CancerMortalityToAge>) coll;

		float maxCancerMortalityInAgeClass_0_14 = 0;
		float maxCancerMortalityInAgeClass_15_39 = 0;
		float maxCancerMortalityInAgeClass_40_44 = 0;
		float maxCancerMortalityInAgeClass_45_49 = 0;
		float maxCancerMortalityInAgeClass_50_54 = 0;
		float maxCancerMortalityInAgeClass_55_59 = 0;
		float maxCancerMortalityInAgeClass_60_64 = 0;
		float maxCancerMortalityInAgeClass_65_69 = 0;
		float maxCancerMortalityInAgeClass_70_74 = 0;
		float maxCancerMortalityInAgeClass_75plus = 0;

		float minCancerMortalityInAgeClass_0_14 = 0;
		float minCancerMortalityInAgeClass_15_39 = 0;
		float minCancerMortalityInAgeClass_40_44 = 0;
		float minCancerMortalityInAgeClass_45_49 = 0;
		float minCancerMortalityInAgeClass_50_54 = 0;
		float minCancerMortalityInAgeClass_55_59 = 0;
		float minCancerMortalityInAgeClass_60_64 = 0;
		float minCancerMortalityInAgeClass_65_69 = 0;
		float minCancerMortalityInAgeClass_70_74 = 0;
		float minCancerMortalityInAgeClass_75plus = 0;

		for (Struct_15_CancerMortalityToAge st : data) {
			if (st.cancerMortalityInAgeClass_0_14 > maxCancerMortalityInAgeClass_0_14)
				maxCancerMortalityInAgeClass_0_14 = st.cancerMortalityInAgeClass_0_14;
			
			if (st.cancerMortalityInAgeClass_15_39 > maxCancerMortalityInAgeClass_15_39)
				maxCancerMortalityInAgeClass_15_39 = st.cancerMortalityInAgeClass_15_39;
			
			if (st.cancerMortalityInAgeClass_40_44 > maxCancerMortalityInAgeClass_40_44)
				maxCancerMortalityInAgeClass_40_44 = st.cancerMortalityInAgeClass_40_44;
			
			if (st.cancerMortalityInAgeClass_45_49 > maxCancerMortalityInAgeClass_45_49)
				maxCancerMortalityInAgeClass_45_49 = st.cancerMortalityInAgeClass_45_49;
			
			if (st.cancerMortalityInAgeClass_50_54 > maxCancerMortalityInAgeClass_50_54)
				maxCancerMortalityInAgeClass_50_54 = st.cancerMortalityInAgeClass_50_54;
			
			if (st.cancerMortalityInAgeClass_55_59 > maxCancerMortalityInAgeClass_55_59)
				maxCancerMortalityInAgeClass_55_59 = st.cancerMortalityInAgeClass_55_59;
			
			if (st.cancerMortalityInAgeClass_60_64 > maxCancerMortalityInAgeClass_60_64)
				maxCancerMortalityInAgeClass_60_64 = st.cancerMortalityInAgeClass_60_64;
			
			if (st.cancerMortalityInAgeClass_65_69 > maxCancerMortalityInAgeClass_65_69)
				maxCancerMortalityInAgeClass_65_69 = st.cancerMortalityInAgeClass_65_69;
			
			if (st.cancerMortalityInAgeClass_70_74 > maxCancerMortalityInAgeClass_70_74)
				maxCancerMortalityInAgeClass_70_74 = st.cancerMortalityInAgeClass_70_74;
			
			if (st.cancerMortalityInAgeClass_75plus > maxCancerMortalityInAgeClass_75plus)
				maxCancerMortalityInAgeClass_75plus = st.cancerMortalityInAgeClass_75plus;
			
			
			if (st.cancerMortalityInAgeClass_0_14 < minCancerMortalityInAgeClass_0_14)
				minCancerMortalityInAgeClass_0_14 = st.cancerMortalityInAgeClass_0_14;
			
			if (st.cancerMortalityInAgeClass_15_39 < minCancerMortalityInAgeClass_15_39)
				minCancerMortalityInAgeClass_15_39 = st.cancerMortalityInAgeClass_15_39;
			
			if (st.cancerMortalityInAgeClass_40_44 < minCancerMortalityInAgeClass_40_44)
				minCancerMortalityInAgeClass_40_44 = st.cancerMortalityInAgeClass_40_44;
			
			if (st.cancerMortalityInAgeClass_45_49 < minCancerMortalityInAgeClass_45_49)
				minCancerMortalityInAgeClass_45_49 = st.cancerMortalityInAgeClass_45_49;
			
			if (st.cancerMortalityInAgeClass_50_54 < minCancerMortalityInAgeClass_50_54)
				minCancerMortalityInAgeClass_50_54 = st.cancerMortalityInAgeClass_50_54;
			
			if (st.cancerMortalityInAgeClass_55_59 < minCancerMortalityInAgeClass_55_59)
				minCancerMortalityInAgeClass_55_59 = st.cancerMortalityInAgeClass_55_59;
			
			if (st.cancerMortalityInAgeClass_60_64 < minCancerMortalityInAgeClass_60_64)
				minCancerMortalityInAgeClass_60_64 = st.cancerMortalityInAgeClass_60_64;
			
			if (st.cancerMortalityInAgeClass_65_69 < minCancerMortalityInAgeClass_65_69)
				minCancerMortalityInAgeClass_65_69 = st.cancerMortalityInAgeClass_65_69;
			
			if (st.cancerMortalityInAgeClass_70_74 < minCancerMortalityInAgeClass_70_74)
				minCancerMortalityInAgeClass_70_74 = st.cancerMortalityInAgeClass_70_74;
			
			if (st.cancerMortalityInAgeClass_75plus < minCancerMortalityInAgeClass_75plus)
				minCancerMortalityInAgeClass_75plus = st.cancerMortalityInAgeClass_75plus;
			
		}

		float m1 = 0;
		float m_y = 0;
		float m_x = 0;

		Collection<Integer> selectedLocations = locationModifier.getLocations();
		
		p.pushStyle();
		
		for (Struct_15_CancerMortalityToAge st : data) {
			p.noFill();
			p.beginShape();
			if(selectedLocations.contains(st.locationId)){
				p.strokeWeight(STROKEWEIGHT_SELECTED);
				p.stroke(COLOR_SELECTED, STROKE_ALPHA_SELECTED);
			}
			else{
				p.strokeWeight(STROKEWEIGHT_NORMAL);
				p.stroke(COLOR_NORMAL, STROKE_ALPHA_NORMAL);
			}
      
			m_x = p.map(0, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_0_14, minCancerMortalityInAgeClass_0_14, maxCancerMortalityInAgeClass_0_14, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(1, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_15_39, minCancerMortalityInAgeClass_15_39, maxCancerMortalityInAgeClass_15_39, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(2, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_40_44, minCancerMortalityInAgeClass_40_44, maxCancerMortalityInAgeClass_40_44, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(3, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_45_49, minCancerMortalityInAgeClass_45_49, maxCancerMortalityInAgeClass_45_49, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(4, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_50_54, minCancerMortalityInAgeClass_50_54, maxCancerMortalityInAgeClass_50_54, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(5, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_55_59, minCancerMortalityInAgeClass_55_59, maxCancerMortalityInAgeClass_55_59, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(6, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_60_64, minCancerMortalityInAgeClass_60_64, maxCancerMortalityInAgeClass_60_64, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(7, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_65_69, minCancerMortalityInAgeClass_65_69, maxCancerMortalityInAgeClass_65_69, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(8, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_70_74, minCancerMortalityInAgeClass_70_74, maxCancerMortalityInAgeClass_70_74, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(9, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerMortalityInAgeClass_75plus, minCancerMortalityInAgeClass_75plus, maxCancerMortalityInAgeClass_75plus, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			p.endShape();
			p.strokeWeight(1);
			p.stroke(0);
		}
		p.popStyle();
		
	}

}
